package com.muyi.controller;

import com.muyi.pojo.entity.Result;
import com.muyi.pojo.entity.Paper;
import com.muyi.pojo.dto.PaperDTO;
import com.muyi.service.PaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "文章模块")
@Slf4j
@RequestMapping("/paper")
@RestController
public class PaperController {
    @Autowired
    private PaperService paperService;

    /**
     * 查询文章数据
     * @return
     */
    @ApiOperation(value = "查询全部文章")
    @GetMapping
    public Result list(){
        log.info("查询全部文章数据");
        //调用service查询文章数据
        List<Paper> paperList =  paperService.list();
        return Result.success(paperList);
    }


    /**
     * 删除文章
     * @return
     */
    @ApiOperation(value = "删除文章")
    @DeleteMapping("/{paper_id}")
    public Result delete(@PathVariable Integer paper_id){
        log.info("根据id删除文章:{}",paper_id);
        //调用service删除文章
        paperService.delete(paper_id);
        return Result.success();
    }


    /**
     * 新增文章
     * @return
     */
    @ApiOperation(value = "新增文章")
    @PostMapping
    public Result add(@RequestBody PaperDTO paperDTO){
        log.info("新增文章: {}" , paperDTO);
        //调用service新增文章
        paperService.add(paperDTO);
        return Result.success();
    }

    @ApiOperation(value = "更新文章")
    @PutMapping
    public Result update(@RequestBody PaperDTO paperDTO){
        log.info("更新文章信息 : {}", paperDTO);
        paperService.update(paperDTO);
        return Result.success();
    }

    // 根据id查询文章
    @ApiOperation(value = "根据文章id查询文章")
    @GetMapping("/{paper_id}")
    public Result getByPaperId(@PathVariable Integer paper_id){
        log.info("根据id查询文章, paper_id:{}",paper_id);
        Paper paper = paperService.getByPaperId(paper_id);
        return Result.success(paper);
    }
}
